package commands;

import java.io.IOException;

import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import util.Utils;
import vo.Comment;

public class PostCommentCommand implements Comando {

	@Override
	public String execute(String[] args) throws IOException{
		if (args.length != 4) {
			return getSintaxe();
		}
		HTable table = new HTable(Comment.TABLENAME);
		Comment comment = new Comment();
		String[] array = {args[1],args[2]};//photoId e userId do usuario que comentou
		byte[] commentId = comment.gerarId(array);
		Put put = new Put(commentId);
		
		byte[] attribute  = Utils.createStringInBytes(Comment.CF_ATTRIBUTES);
		byte[] texto  = Utils.createStringInBytes(Comment.COMMENT_TEXT);
		
		put.add(attribute, texto, Bytes.toBytes(args[3]));
		
		table.put(put);	
		
		String id = new String(commentId);
		return id;
	}

	@Override
	public String getKey() {
		return "-postComment";
	}

	@Override
	public String getSintaxe() {
		return "-postComment userId photoId text";
	}

}
